package com.industrial.operation.controller;

import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.operation.domain.OperationGdp;
import com.industrial.operation.domain.OperationHighQuality;
import com.industrial.operation.service.IOperationHighQualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;


/**
 * 国家高质量发展综合绩效评价Controller
 *
 * @author ZhouS
 * @date 2024-03-27
 */
@RestController
@RequestMapping("/quality")
public class OperationHighQualityController extends BaseController
{
    @Autowired
    private IOperationHighQualityService operationHighQualityService;

    /**
     * 查询国家高质量发展综合绩效评价列表
     */

    @GetMapping("/list")
    public TableDataInfo list(OperationHighQuality operationHighQuality)
    {
        startPage();

        List<OperationHighQuality> list = operationHighQualityService.selectOperationHighQualityList(operationHighQuality);
        return getDataTable(list);
    }

    /**
     * 导出国家高质量发展综合绩效评价列表
     */
    @RequiresPermissions("@ss.hasPermi('industrial:quality:export')")
    @Log(title = "国家高质量发展综合绩效评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperationHighQuality operationHighQuality)
    {
        List<OperationHighQuality> list = operationHighQualityService.selectOperationHighQualityList(operationHighQuality);
        ExcelUtil<OperationHighQuality> util = new ExcelUtil<OperationHighQuality>(OperationHighQuality.class);
        util.exportExcel(response, list, "国家高质量发展综合绩效评价数据");
    }

    /**
     * 获取国家高质量发展综合绩效评价详细信息
     */
    @RequiresPermissions("@ss.hasPermi('industrial:quality:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(operationHighQualityService.selectOperationHighQualityByBaseId(baseId));
    }

    /**
     * 新增国家高质量发展综合绩效评价
     */
    @RequiresPermissions("@ss.hasPermi('industrial:quality:add')")
    @Log(title = "国家高质量发展综合绩效评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperationHighQuality operationHighQuality)
    {
        return toAjax(operationHighQualityService.insertOperationHighQuality(operationHighQuality));
    }

    /**
     * 修改国家高质量发展综合绩效评价
     */
    @RequiresPermissions("@ss.hasPermi('industrial:quality:edit')")
    @Log(title = "国家高质量发展综合绩效评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperationHighQuality operationHighQuality)
    {
        return toAjax(operationHighQualityService.updateOperationHighQuality(operationHighQuality));
    }

    /**
     * 删除国家高质量发展综合绩效评价
     */
    @RequiresPermissions("@ss.hasPermi('industrial:quality:remove')")
    @Log(title = "国家高质量发展综合绩效评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(operationHighQualityService.deleteOperationHighQualityByBaseIds(baseIds));
    }

    @GetMapping(value = "/groupYears")
    public AjaxResult groupYears(String years)
    {
        OperationHighQuality operationHighQuality =new OperationHighQuality();
        if(StringUtils.isNotNull(years)){
            operationHighQuality.setYears(years);
        }
        return success(operationHighQualityService.groupYears(operationHighQuality));
    }
    // 下载模板
    @GetMapping("/importTemplate/国家高质量发展综合绩效评价")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<OperationHighQuality> util = new ExcelUtil<OperationHighQuality>(OperationHighQuality.class);
        util.importTemplateExcel(response, "国家高质量发展综合绩效评价","国家高质量发展综合绩效评价");
    }
    /**
     * 导入
     */
    @Log(title = "国家高质量发展综合绩效评价", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {
        try {
            int flag=0;

                ExcelUtil<OperationHighQuality> util = new ExcelUtil<OperationHighQuality>(OperationHighQuality.class);
                List<OperationHighQuality> list = util.importExcel(file.getInputStream(),1);

                for (int i = 0; i < list.size(); i++) {
                    List<OperationHighQuality> olist=  operationHighQualityService.byYears(list.get(i).getYears());
                    if(olist.size()>0){
                        System.out.println("重复年份"+list.get(i).getYears());
                    }else{
                        String id= IdUtils.simpleUUID();
                        list.get(i).setBaseId(id);
                        flag+=  operationHighQualityService.insertOperationHighQuality(list.get(i));
                    }
                }

            return AjaxResult.success("成功导入"+flag+"条数据！");
         }catch (Exception e){
                logger.error("抛出"+e);
                return AjaxResult.error("错误");
         }
    }

}
